#include "statustypedata.h"
#include "statustype.h"
#include "status.h"
#include "statustypemanager.h"

#include <QtAlgorithms>
#include <QIcon>


StatusTypeManager * StatusTypeManager::m_instance = NULL;

StatusTypeManager * StatusTypeManager::instance()
{
    if (NULL == m_instance)
        m_instance = new StatusTypeManager();
    return m_instance;
}


StatusTypeManager::StatusTypeManager()
{
    m_statusTypes.insert(StatusTypeFreeForChat,
                         StatusTypeData(StatusTypeFreeForChat,
                                        "FreeForChat",
                                        "Free for chat",
                                        "free-for-chat",
                                        StatusTypeGroupOnline));
    m_statusTypes.insert(StatusTypeOnline,
                         StatusTypeData(StatusTypeOnline,
                                        "Online",
                                        "Online",
                                        "online",
                                        StatusTypeGroupOnline));
    m_statusTypes.insert(StatusTypeAway,
                         StatusTypeData(StatusTypeAway,
                                        "Away",
                                        "Away",
                                        "away",
                                        StatusTypeGroupAway));
    m_statusTypes.insert(StatusTypeNotAvailable,
                         StatusTypeData(StatusTypeNotAvailable,
                                        "NotAvailable",
                                        "Not available",
                                        "not-available",
                                        StatusTypeGroupAway));
    m_statusTypes.insert(StatusTypeDoNotDisturb,
                         StatusTypeData(StatusTypeDoNotDisturb,
                                        "DoNotDisturb",
                                        "Do not disturb",
                                        "do-not-disturb",
                                        StatusTypeGroupAway));
    m_statusTypes.insert(StatusTypeInvisible,
                         StatusTypeData(StatusTypeInvisible,
                                        "Invisible",
                                        "Invisible",
                                        "invisible",
                                        StatusTypeGroupInvisible));
    m_statusTypes.insert(StatusTypeOffline,
                         StatusTypeData(StatusTypeOffline,
                                        "Offline",
                                        "Offline",
                                        "offline",
                                        StatusTypeGroupOffline));
}


StatusTypeManager::~StatusTypeManager()
{
}


StatusType StatusTypeManager::fromName(const QString &name)
{
    for (QMap<StatusType, StatusTypeData>::const_iterator it = m_statusTypes.constBegin(), \
         end = m_statusTypes.constEnd(); it != end; ++it)
        if (it.value().name() == name)
            return it.key();
    return StatusTypeOffline;
}


const StatusTypeData StatusTypeManager::statusTypeData(StatusType statusType)
{
    if (m_statusTypes.contains(statusType))
        return m_statusTypes.value(statusType);
    return m_statusTypes.value(StatusTypeOffline);
}


QIcon StatusTypeManager::statusIcon(const Status &status)
{
    const StatusTypeData &statusTypeData = this->statusTypeData(status.type());

    QString iconName = QString("status-icon/%1/%2%")
            .arg(statusTypeData.iconName())
            .arg(status.description().isEmpty() ? QString() : QLatin1String("_d"));

    return QIcon(iconName);
}
